﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DotNet.Common.Kit;
using System.IO;
using Shop.Image.Kit;
using Microsoft.VisualBasic.FileIO;
namespace NetShopImage
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问,可以与JS通信
    public partial class Form1 : Form
    {
        List<ImgEntity> _listImgEnt = new List<ImgEntity>();
        string _lsbIndex="";//点击Listbox索引值及菜单类型。 如删除菜单 1+索引号 12表示点删除，且第二项  2向上， 3向下
        HtmlDocHelper _htmlDoc = new HtmlDocHelper(Application.StartupPath + "\\head.htm", Application.StartupPath + "\\foot.htm");
        string _html = "";  //得到的HTML内容部分
        string _imgStyle = "";
        string _imgAlt = "";

        string _iniFilePath = Application.StartupPath + "\\sys.ini";
        string _dirFilePath = "";

        public Form1()
        {
            InitializeComponent();
            webBrowser1.ObjectForScripting = this;  //可以与JS进行通信
        }

        #region  按钮产生代码区域
        private void btn_Loc(object sender, EventArgs e)
        {
            if (cmb_DirList.Text != "")
            {
                int imgIndex=1;
                _html = "";
                lsbImg.Items.Clear();
                foreach(ImgEntity img in _listImgEnt)
                {
                    img.ImgName = ImgEntity.RenameImg(imgIndex, ".jpg");
                    string destFileName = _dirFilePath +"\\"+ cmb_DirList.Text + "\\images\\"+img.ImgName;
                    File.Copy(img.ImgFullName,destFileName,true);
                    _html += _htmlDoc.CreateImgDom(destFileName,img.ImgName, _imgAlt, _imgStyle);
                    lsbImg.Items.Add(img.ImgName);
                    imgIndex++;
                }
                webBrowser1.DocumentText=_htmlDoc.GetHtmlDoc(_html);
                MessageBox.Show("生成页面成功");
                //webBrowser1.Document.Body.InnerHtml = _html;
            }
            else
            {
                MessageBox.Show("请选择一个类型编号");
            }
        }
        private void btn_ivy_Click(object sender, EventArgs e)
        {
            if (cmb_DirList.Text != "")
            {
                TaoBaoImage tb = new TaoBaoImage(_iniFilePath, "木子李ivy", cmb_DirList.Text);
                int imgIndex = 1;
                _html = "";
                foreach (ImgEntity img in _listImgEnt)
                {
                    img.ImgName = ImgEntity.RenameImg(imgIndex, ".jpg");
                    img.ImgUrl = tb.ImageUpload(img.ImgFullName, img.ImgName);
                    _html += _htmlDoc.CreateImgDom(img.ImgUrl, img.ImgName, _imgAlt, _imgStyle);
                    imgIndex++;
                }
                webBrowser1.DocumentText = _htmlDoc.GetHtmlDoc(_html);
                MessageBox.Show("生成页面成功");
            }
            else
            {
                MessageBox.Show("请选择一个类型编号");
            }

        }
        private void btn_lhl_Click(object sender, EventArgs e)
        {
            if (cmb_DirList.Text != "")
            {
                TaoBaoImage tb = new TaoBaoImage(_iniFilePath, "lhl62411570", cmb_DirList.Text);
                int imgIndex = 1;
                _html = "";
                foreach (ImgEntity img in _listImgEnt)
                {
                    img.ImgName = ImgEntity.RenameImg(imgIndex, ".jpg");
                    img.ImgUrl = tb.ImageUpload(img.ImgFullName, img.ImgName);
                    _html += _htmlDoc.CreateImgDom(img.ImgUrl, img.ImgName, _imgAlt, _imgStyle);
                    imgIndex++;
                }
                webBrowser1.DocumentText = _htmlDoc.GetHtmlDoc(_html);
                MessageBox.Show("生成页面成功");
            }
            else
            {
                MessageBox.Show("请选择一个类型编号");
            }
        }
        private void btn_tgh_Click(object sender, EventArgs e)
        {
            if (cmb_DirList.Text != "")
            {
                TaogonghuoImage tgh = new TaogonghuoImage(_iniFilePath, "tgh", cmb_DirList.Text);
                int imgIndex = 1;
                _html = "";
                foreach (ImgEntity img in _listImgEnt)
                {
                    img.ImgName = ImgEntity.RenameImg(imgIndex, ".jpg");
                    img.ImgUrl = tgh.ImageUpload(img.ImgFullName, img.ImgName);
                    _html += _htmlDoc.CreateImgDom(img.ImgUrl, img.ImgName, _imgAlt, _imgStyle);
                    imgIndex++;
                }
                webBrowser1.DocumentText = _htmlDoc.GetHtmlDoc(_html);
                MessageBox.Show("生成页面成功");
            }
            else
            {

            }
        }

        #endregion

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    // 调用JavaScript的messageBox方法，并传入参数

        // //   string iniFilePath = Application.StartupPath + "\\sys.ini";

        // //   TaogonghuoImage tgh =new TaogonghuoImage(iniFilePath,"tgh","4560");
        // //   string url=tgh.ImageUpload("d:\\1.jpg", "xXXXx.jpg");
        // //   string img=htmlDoc.CreateImgDom(url, "", "");
        // //   string htm = htmlDoc.GetHtmlDoc(img);
            
        // //   webBrowser1.DocumentText = htm;
        // //  // TaoBaoImage tb = new TaoBaoImage(iniFilePath, "lhl62411570", "123000");
        // ////   string url=tb.ImageUpload("d:\\1.jpg", "xXXXx.jpg");
        // //   //string img="<img src='"+"d:\\0.jpg"+"'/>";
        // //  // string img1 = "<img src='" + "http://img03.taobaocdn.com/imgextra/i3/88184695/T2UptjXAXaXXXXXXXX_!!88184695.jpg" + "'/>";
        // //  // webBrowser1.Document.CreateElement(img);
        // // //  webBrowser1.Url = new Uri("http://www.baidu.com");
        // //  // Uri uri = new Uri("http://www.baidu.com");
        // //   //HtmlDocument html;
        // //   //webBrowser1.DocumentText = img;
        // //   //webBrowser1.Dispose();
        //}

        #region ListBox事件处理部分代码
        private void lsbImg_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
            {
                String[] files = (String[])e.Data.GetData(DataFormats.FileDrop);
                
                foreach (String s in files)
                {
                    if (s.Contains(".jpg") || s.Contains(".gif") || s.Contains(".png"))
                    {
                        ImgEntity ImgEnt = new ImgEntity();
                        ImgEnt.ImgFullName = s;
                        ImgEnt.ImgName = s.Split('\\')[s.Split('\\').Length-1];
                        if (!lsbImg.Items.Contains(ImgEnt.ImgName))
                        {
                            Image img = Image.FromFile(s);
                            ImgEnt.ImgWidth = img.Width;
                            ImgEnt.ImgHeight = img.Height;
                            _html += _htmlDoc.CreateImgDom(s, ImgEnt.ImgFullName, _imgAlt, _imgStyle);
                            _listImgEnt.Add(ImgEnt);
                            lsbImg.Items.Add(ImgEnt.ImgName);
                        }
                    }
                }
                webBrowser1.DocumentText = _htmlDoc.GetHtmlDoc(_html); //加载完成后调用DocumentComplete方法
            }
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            object[] objects = new object[1];
            webBrowser1.ObjectForScripting = this;  //可以与JS进行通信
            if (_lsbIndex == "")
            {
                objects[0] = _listImgEnt[0].ImgName;
                lsbImg.SelectedIndex = lsbImg.Items.IndexOf(_listImgEnt[0].ImgName);
            }
            else
            {
                string op = _lsbIndex.Split(':')[0];
                int selIndex = Convert.ToInt32(_lsbIndex.Split(':')[1]);
                if (op == "1")  //删除操作
                {
                    if (selIndex < lsbImg.Items.Count) //中间的某一项
                    {
                        objects[0] = _listImgEnt[selIndex].ImgName;
                        lsbImg.SelectedIndex = lsbImg.Items.IndexOf(_listImgEnt[selIndex].ImgName);
                    }
                    else
                    {
                        if (selIndex > 0)
                        {
                            objects[0] = _listImgEnt[selIndex - 1].ImgName;
                            lsbImg.SelectedIndex = lsbImg.Items.IndexOf(_listImgEnt[selIndex - 1].ImgName);
                        }
                    }
                }
                else if (op == "2")  //向上移到
                {
                    objects[0] = _listImgEnt[selIndex - 1].ImgName;
                    lsbImg.SelectedIndex = lsbImg.Items.IndexOf(_listImgEnt[selIndex - 1].ImgName);
                }
                else   //向下移动
                {
                    objects[0] = _listImgEnt[selIndex + 1].ImgName;
                    lsbImg.SelectedIndex = lsbImg.Items.IndexOf(_listImgEnt[selIndex + 1].ImgName);
                }
            }
            webBrowser1.Document.InvokeScript("ScrollYd", objects);
        }

       /// <summary>
       /// 在JS代码中调用 得到选择的项目。
       /// </summary>
       /// <param name="imgFileName"></param>
        public void ListBoxSelect(string imgFileName)
        {
            int index=lsbImg.Items.IndexOf(imgFileName);
            lsbImg.SelectedIndex = index ;
        }

        private void lsbImg_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        /// <summary>
        /// 与JS通信点击时在WebBrowser上标识选中的项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lsbImg_MouseDown(object sender, MouseEventArgs e)
        {
            int index = lsbImg.IndexFromPoint(e.X, e.Y);
            if (index != -1)
            {
                if (e.Button == MouseButtons.Left)
                {
                    object[] objects = new object[1];
                    objects[0] = lsbImg.Items[index];
                    webBrowser1.Document.InvokeScript("ScrollYd", objects);
                }
                if (e.Button == MouseButtons.Right)
                {
                    cms_lsb.Show(lsbImg, e.Location);
                }
            }
        }
        #endregion

        #region 弹出菜单处理部分
       /// <summary>
       /// 重新绑定数据到WebBrowser中
       /// </summary>
        private void ReBindWebBrowser()
        {
            webBrowser1.DocumentText = "";   //清除2个变量。重新再绑定
            _html = "";
            foreach (ImgEntity img in _listImgEnt)
            {
                _html += _htmlDoc.CreateImgDom(img.ImgFullName, img.ImgName, _imgAlt, _imgStyle);
            }
            webBrowser1.DocumentText = _htmlDoc.GetHtmlDoc(_html);
        }
        private void DelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int position = lsbImg.SelectedIndex;
            _lsbIndex = "1:"+position.ToString();
            lsbImg.Items.RemoveAt(position);
            _listImgEnt.RemoveAt(position);
            
            ReBindWebBrowser();
        }

        private void 向上移动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int position = lsbImg.SelectedIndex;
            _lsbIndex = "2:" + position.ToString();
            if (lsbImg.Items.Count >= 2)
            {
                if (position >= 1)
                {
                    string value = lsbImg.SelectedItem.ToString();//交换listbox中的元素
                    lsbImg.Items.RemoveAt(position);
                    lsbImg.Items.Insert(position - 1, value);

                    _listImgEnt.Reverse(position - 1, 2);  //将两个相邻的结点交换位置。向上移动时则以当前结的前一个为起点，
                    ReBindWebBrowser();
                }
            }
        }

        private void 向下移动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int position = lsbImg.SelectedIndex;
            _lsbIndex = "3:" + position.ToString();
            if (lsbImg.Items.Count >= 2)//至少有两个元素上下操作才有意义
            {
                if (position < lsbImg.Items.Count - 1)
                {
                    string value = lsbImg.SelectedItem.ToString();//交换listbox中的元素
                    lsbImg.Items.RemoveAt(position);
                    lsbImg.Items.Insert(position + 1, value);

                    _listImgEnt.Reverse(position, 2);  //将两个相邻的结点交换位置。向下移动时，则以当前结点索引为起点和下一个结点反序。
                    ReBindWebBrowser();
                }
            }
        }
        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            IniHelper ini = new IniHelper(_iniFilePath);
            _dirFilePath = ini.GetKeyValue("filepath", "savepath");
            _imgStyle = ini.GetKeyValue("img", "style");
            _imgAlt = ini.GetKeyValue("img", "alt");
            if (Directory.Exists(_dirFilePath))
            {
                string[] dirList = Directory.GetDirectories(_dirFilePath, "1*");
                foreach (string dirName in dirList)
                {
                    cmb_DirList.Items.Add(dirName.Split('\\')[3]);
                }
            }
            else
            {
                MessageBox.Show("没有配置好保存图片的文件夹路径,请检查INI文件或者磁盘路径");
            }
        }

        private void btnIni_Click(object sender, EventArgs e)
        {
            if (txtIni.Text != "" && cmbIni.Text != "")
            {
                IniHelper ini = new IniHelper(_iniFilePath);
                string section = cmbIni.Text;
                if (ini.SetKeyValue(section, "sessionKey", txtIni.Text))
                {
                    MessageBox.Show("Ini文件SessionKey字段设置成功");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TaoBaoImage taobao = new TaoBaoImage("lhl62411570");
            taobao.GetShopItems();
        }

    }
}
